/**
 * Created by seeker910 on 2014/8/20.
 */
Rsd.define('Rsd.form.Link', {
    extend: 'Rsd.control.Component',
    requires:['Rsd.tool.LinkSelector'],
    xtype: 'link',
    margin:'0 0 0 0',
    text: '',
    cls:'x-link',
    domTagName:'span',
    ctrlTagName: 'a',
    height:30,
    href:'javascript:void(0)',//http://www.baidu.com
    target:'_blank',
    format:null,
    preventDefault:true, 
    schema:{ 
        title: '链接',
        type: 'object',
        properties: {
            "id": {
                type: 'string',
                title: '键值'
            },
            "text": {
                type: 'string',
                title: '文字'
            },
            "href": {
                type: 'string',
                title: '链接'
            },
            "label": {
                type: 'string',
                title: '标签文字',
                hidden: true

            },
            "height": {
                type: 'string',
                title: '高度',
                hidden: true
            },
            "width": {
                type: 'string',
                title: '宽度',
                hidden: true
            },
            "flex": {
                type: 'number',
                title: '弹性',
                hidden: true
            },
            "liStyle": {
                type: 'object',
                title: '列表元素样式',
                hidden: true,
                properties:{
                    flex:{
                        type: 'number',
                        title: '弹性',
                        hidden: true
                    }
                }
            }
        },
        "required": ["text", "href"],
        viewTemplate:{
            //link 最list控件子元素时，编辑模板
            'listItemEditView':{
                xtype:'json-form',
                formEventHandler:'formEventHandler',//表单编辑时发生,该响应方法DomEditor中实现
                width:'100%',
                height:210,
                layout:{type:'vbox',align:'start'}, 
                items:[
                     {
                        xtype: 'text',
                        margin: '0 0 2 10',
                        width: '80%',
                        label: '键 值',
                        readonly:true,
                        onchange : "onFieldValueChange",//json form filed 编辑事件
                        dataIndex: 'id',
                        value: '{{=id}}'
                    },
                    {
                        xtype: 'text',
                        margin: '0 0 2 10',
                        width: '80%',
                        label: '文 字',
                        onchange : "onFieldValueChange",//json form filed 编辑事件
                        dataIndex: 'text',
                        value: '{{=text}}'
                    },
                    {
                        xtype: 'link-selector',
                        margin: '0 0 2 10',
                        width: '80%',
                        label: '链 接',
                        onchange : "onFieldValueChange",
                        dataIndex: 'href',
                        value: '{{=href}}',
                    },
                    {
                        xtype: 'text',
                        margin: '0 0 2 10',
                        width: '80%',
                        label: '宽 度',
                        onchange : "onFieldValueChange",
                        dataIndex: 'width',
                        value: '{{=width}}'
                    },
                    {
                        xtype: 'text',
                        margin: '0 0 2 10',
                        width: '80%',
                        label: '高 度',
                        onchange : "onFieldValueChange",
                        dataIndex: 'height',
                        value: '{{=height}}'
                    },
                    {
                        xtype: 'number',
                        margin: '0 0 2 10',
                        width: '80%',
                        label: '弹 性',
                        onchange : "onFieldValueChange",
                        dataIndex: 'liStyle.flex',
                        value: '{{=liStyle.flex}}'
                    }
                ]
                
            }
        }
    }, 
    /*
    * */
    constructor: function Link(config) {
        config = config || {}; 
        this.apply(config);  
    } ,
    /**
     * 
     */
    onAfterInit: function onAfterInit() {

        if (this.text) {
            var nodes = Rsd.parseDom(this.text);
            for(var i = 0; i < nodes.length;i++)
            {
                this.ctrl.element.appendChild(nodes[i]);
            } 
            //this.ctrl.element.appendChild(document.createTextNode(this.text));
        }
        if(this.href)
        {
            this.setHref(this.href);
        }
        this.callParent();
    },
    /**
     * 
     */
    onAfterLayout:function onAfterLayout()
    {
        if(Rsd.isNumber(Number(this.height)))
        {
            if(this.ctrl.element)
            {
                this.ctrl.element.style.lineHeight = this.height+ 'px';
            } 
            if(this.label.element)
            {
                this.label.element.style.lineHeight = this.height+ 'px';
            }
            
        }
        this.callParent();
    },
    /**
     * 
     * @param {*} href 
     * @param {*} target 
     */
    setHref:function setHref(href,target)
    {
        this.href = href;
        this.setElAttribute(this.ctrl.element,'href',this.href);
        this.setElAttribute(this.ctrl.element,'target',target||this.target||"_blank");  
    },
    /*
     * */
    getText: function getText() {
        return  this.text;
    },
    /*
     * */
    setText: function setText(text) {
        var me = this;
        me.text = text || '';
        if (me.ctrl.element) {
            me.ctrl.element.innerHTML = '';
            //while(me.ctrl.element.childNodes.length > 0)
            //{
            //    me.ctrl.element.remove(me.ctrl.element.childNodes[0]);
            //}
            var nodes = Rsd.parseDom(this.text);
            for(var i = 0; i < nodes.length;i++)
            {
                me.ctrl.element.appendChild(nodes[i]);
            }
            //me.ctrl.element.appendChild(document.createTextNode(me.text));

        }
    },
    /*
    * 返回 对象或数组
    * */
   makeControl:function makeControl(config,row)
   {
       var _config = config ||{};
       var _editable = _config.editable;
       var _dataIndex = _config.dataIndex|| _config.dataindex || _config.name;
       //var _value = row[_dataIndex];
       _config.__index__= _config.__index__||0;
       
       var ctrls = []; 

       if(_editable)
       { 
       }
       else
       {
           if(row[_dataIndex])
           {
                var _link = document.createElement('a');
            
                _link.setAttribute('name',_dataIndex);
                _link.setAttribute('href',row[_dataIndex]);
                _link.setAttribute('target',"_blank");
                _link.setAttribute('title',_config.tip||'点击打开链接');
                _link.appendChild(document.createTextNode(row[_dataIndex]));
                ctrls.push(_link); 
           }
           else
           { 
                ctrls.push(document.createTextNode(config.emptyText)); 
           }
            
       }

       return ctrls;

   }
});
